/* Loading */
function showLoading(msg) {
  return layer.msg(msg ? msg : "加载中...", {
    icon: 16,
    time: 0,
    shade: [0.8, "#000"],
  });
}

/**
 * 对jQuery的ajax方法再次封装
 *
 * 用法
 * Ajax('data.json', {}, 'POST', function(data){
 *   console.log(data);
 * })
 */
function Ajax(url, data, type, cb) {
  var loadT = showLoading("数据加载中...");

  $.ajax({
    url: url,
    type: type,
    dataType: "json",
    contentType: "application/json",
    data: data,
  })
    .done(function (result, textStatus) {
      layer.close(loadT);
      console.log("Ajax:", result);
      if (result.code == 0) {
        typeof cb == "function" && cb(result.data);
      } else {
        layer.msg(result.msg, { icon: 2 });
      }
    })
    .fail(function (jqXHR, textStatus) {
      layer.msg("服务出错了~", { icon: 2 });
    });
}

function GetJSON(url, data, cb) {
  // var loadT = showLoading("数据加载中...");

  $.getJSON(url, data)
    .done(function (result, textStatus) {
      // layer.close(loadT);
      if (result.code == 0) {
        // console.log("Ajax:", result);
        typeof cb == "function" && cb(result.data);
      } else {
        layer.msg(result.msg, { icon: 2 });
      }
    })
    .fail(function (jqXHR, textStatus) {
      layer.msg("服务出错了~", { icon: 2 });
    });
}
/**
 * jQuery Echart 插件
 *
 * 用法
 * $(el).echartSet(options);
 */
!(function ($) {
  var timer;
  $.fn.echartSet = function (option) {
    return this.each(function () {
      clearTimeout(timer);
      var ec = echarts.init($(this)[0], "theme"),
        timer;
      ec.setOption(option);
      $(window).resize(function () {
        timer = setTimeout(ec.resize, 100);
      });
    });
  };
})(jQuery);

/* 关闭弹出框口 */
function layerClose() {
  var index = parent.layer.getFrameIndex(window.name);
  parent.layer.close(index);
}

// 获取当前周
function getWeek(week_num) {
  var weekData = ["周日", "周一", "周二", "周三", "周五", "周六"];
  return weekData[week_num];
}

// 获取多少数组
function changeDataLen(data, len) {
  if (data.length > len) {
    return data.filter(function (item, key) {
      return key < len;
    });
  } else {
    return data;
  }
}
